package cn.test.controller;

import cn.test.bean.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Validated
public class UserController {
	@Autowired
	private Validator validator;

	@RequestMapping("/**/index")
	@ResponseBody
	public String tessdt(){
		System.out.println("请求了**匹配");
		return "1";
	}

	@RequestMapping("/{path:.+}hehehe")
	@ResponseBody
	public String sdf(){
		System.out.println("请求了正则匹配");
		return "1";
	}
	/**
	 * 这里加上bindingresult可以注入，然后不会直接抛出异常，需要自己处理
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/t",method = RequestMethod.GET)
	public String addUser(@Valid UserValidator user) {
		System.out.println(user.getName());
		return "/index.html";
	}

	@ResponseBody
	@RequestMapping(value = "validString", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)
	public String validString(
			@RequestParam(value = "str", defaultValue = "")
			@Size(min = 1, max = 3)
			String vStr){

		return vStr;
	}

	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public Map test() {
		Map m = new HashMap();
		m.put("a", 123);
		m.put("b", "zxc");
		return m;
	}

}
